//
//  NewBuyListVC.swift
//  LeGou
//
//  Created by 刘亿前 on 2022/9/16.
//

import UIKit

class NewBuyListVC: ZMViewController {

    lazy var listTable: UITableView = {
        let table = UITableView(frame: CGRect(), style: .plain)
        table.delegate = self
        table.dataSource = self
        table.separatorStyle = .none
        table.register(UINib(nibName: "NewBuyListCell", bundle: nil), forCellReuseIdentifier: "NewBuyListCell")
        table.showsVerticalScrollIndicator = false
        table.backgroundColor = .clear
        table.tableFooterView = UIView()
        table.rowHeight = 132
        view.addSubview(table)
        table.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        addRefresh(scrollView: table) { [unowned self] refresh in
            self.page = refresh ?  1 : self.page + 1
            self.requestList()
        }
        table.mj_footer?.isHidden = true
        return table
    }()
    
    var page = 1
    // 商品分类ID
    var cid = 0
    let pageSize = 10
    var dataArr = [TBGoods]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .clear
        emptyImg = "nodata"
        emptyTitle = "暂无数据"
        glt_scrollView = listTable
        listTable.mj_header?.beginRefreshing()
    }

}

extension NewBuyListVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NewBuyListCell", for: indexPath) as! NewBuyListCell
        cell.setUI(dataArr[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = TbDetailVC()
        vc.newbuy = true
        vc.id = dataArr[indexPath.row].goodsId
        navigationController?.pushViewController(vc, animated: true)
//        let data = dataArr[indexPath.row]
//        router.push("xm://main/Home/tb/detail", context: ["id": data.goodsId, "cid": cid])
    }
}


// MARK: - Request Action
extension NewBuyListVC {
    func requestList() {
        var pa = ["limit": pageSize, "page": page, "typeEnum": "FIRST_ORDER"] as [String : Any]
        if cid > 0 {
            pa["cid"] = cid
        }
        
        ZMUtil.listRequest(type: TBGoods.self, api: HomeAPI.tb_goods_list(pa)) { [unowned self] resp in
            if resp.code == .success, let data = resp.data {
                self.relodaData(data)
            } else {
                self.relodaData()
            }
        }
    }
    
    func relodaData(_ data: [TBGoods] = []) {
        addNodataScrollView(scrollView: listTable)
        if page == 1 {
            dataArr.removeAll()
        }
        dataArr.append(contentsOf: data)
        listTable.mj_header?.endRefreshing()
        listTable.mj_footer?.endRefreshing()
        if data.count < pageSize {
            listTable.mj_footer?.endRefreshingWithNoMoreData()
        }
        listTable.mj_footer?.isHidden = dataArr.count == 0
        listTable.reloadData()
    }
}
